package com.qf.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.qf.entity.Data;
import com.qf.entity.MsgCode;
import com.qf.entity.SmsCode;
import com.qf.utils.MsgCodeFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class MsgCodeController {

    @RequestMapping("sms")
    public Map sendMsgCode(HttpServletRequest req, HttpServletResponse resp, String phone) {

        //获取前端发送的手机号
        String phoneNum = phone;
        //发送到前端的结果
        Map<String, Object> map = new HashMap<>();

        //如果手机号为空就不发送
        if (phoneNum == null || phoneNum.trim().equals("")) {
            map.put("code", "1");
            map.put("msg", "手机号格式不正确，发送验证码失败");
            return map;
        }

        //生成4位随机数作为验证码
        String verificationCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
        System.out.println(verificationCode);
        //生成验证码对象
        MsgCodeFactory msgCodeFactory = new MsgCodeFactory();
        //发送验证码，获取返回结果
        String code = null;
        String msg = null;
        SendSmsResponse Smsresponse = null;
        try {
            Smsresponse = msgCodeFactory.sendSms(phoneNum, verificationCode);
            code = Smsresponse.getCode();
            msg = Smsresponse.getMessage();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        //  阿里接口发生错误的情况
        if (!code.equals("OK") || !msg.equals("OK")) {
            map.put("code", "2");
            map.put("msg", "服务器内部错误，发送验证码失败");
            return map;
        }

        //把验证码内容(验证码内容，过期时间)存到session中作为判断
        Date expireTime = new Date(new Date().getTime() + 300000);
        SmsCode smsCode = new SmsCode(verificationCode, phoneNum, 120);
        MsgCode msgCode = new MsgCode(verificationCode, expireTime);
        //从session中取这个值，用于判断验证是否正确和过期
        req.getSession().setAttribute("msgCode", msgCode);
        req.getSession().setAttribute("smsCode", smsCode);
        map.put("code", "3");
        map.put("msg", "发送验证码成功");
        return map;
    }

    @RequestMapping("CheckCodeAndPhone")
    public Data CheckCodeAndPhone(HttpServletRequest req, HttpServletResponse resp, String phone, String validateCode) {

        //获取前端输入的电话和验证码
//        String phone = req.getParameter("phone");
        String checkCode = validateCode;

        //获取短信验证码session
        HttpSession session = req.getSession();
        SmsCode smscode = (SmsCode) session.getAttribute("smsCode");
        MsgCode msgcode = (MsgCode) session.getAttribute("msgCode");

        //3种情况
        Data data = null;
        //1.若没有输入验证码或者验证码输错
        if (null == checkCode || null == smscode || !smscode.getCode().equals(checkCode)) {
            data = new Data(-3, "验证码输入错误");
            return data;
        }
        //2.验证码时间过了
        //msgcode.getExpireTime()表示过期时间
        //a.before（b）,a比b小返回true，
        //倘若过期时间比当前时间小（早），表示已过期
        if (msgcode.getExpireTime().before(new Date())) { //验证码过期了
            data = new Data(-2, "验证码过期.");
            return data;
        }
        //3.验证码对了 但手机号是错的
        if (smscode.getCode().equals(checkCode) && !smscode.getPhone().equals(phone)) {
            data = new Data(-1, "电话号码输入错误");
            return data;
        }

        data = new Data(1, "输入正确");
        return data;

    }

}
